<?php
class Product_Model_OrderPickup
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_OrderPickup();
	}

	public  function create($row){
 		return $this->_table->insert($row); 
	}
	
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('op_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
	
	public  function delete($id){
		$where = $this->_table->getAdapter()->quoteInto('op_id = ?', $id);
		return $this->_table->delete($where);
	}
	public  function deleteByOpCode($opCode){
		$where = $this->_table->getAdapter()->quoteInto('op_code = ?', $opCode);

		return $this->_table->delete($where);
	}
	public function getByField($field,$value){
		$select = $this->_table->select()->where("{$field} = ?", $value);
		return $this->_table->fetchAll($select);
	}
	
	public function getMaxByCurrentDate(){
		$select = $this->_table->select()->where("op_create_date >= ?", date("Y-m-d 00:00:00"));
		$select->order("op_id desc");
		return $this->_table->fetchAll($select);
	}
	
	public  function getByCondition($condition=array(),$type='*',$page="",$pageSize=""){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
       
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(!empty($condition['op_code'])){
			$select->where("op_code=?",$condition["op_code"]);
		}
		
		if(isset($condition['op_status']) && $condition['op_status']>=0){
			$select->where("op_status=?",$condition["op_status"]);
		}
		if(isset($condition['orderType']) && $condition['orderType']==1){
			$select->where("op_order_cnt=op_product_cnt");
		}elseif(isset($condition['orderType']) && $condition['orderType']==2){
			$select->where("op_order_cnt!=op_product_cnt");
		}
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
        	$select->order("op_create_date desc");
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
			$sql = str_replace("*", "*,case when op_order_cnt=op_product_cnt then 1 else 0 end as one_piece", $sql);
// 			echo $sql;exit;
			
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
}